﻿using DataLibrary;
using DataLibrary.Utility;
using KOLTV.Models;
using KOLTV.MVCUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Web.Mvc;
using System.Web.UI;
using System.Web.WebPages;

namespace KOLTV.Controllers
{
    public class HomeController : BaseController
    {
        public ActionResult Index()
        {
            string excelPath = Server.MapPath(@"~/App_Data/KOL账号推及收视频道_20231019031802.xlsx") ;

            string excelPath2 = Server.MapPath(@"~/App_Data/收视频道推及KOL账号_20231019031809.xlsx") ;


            //string connect = @"Data Source=10.20.66.203;Initial Catalog=DataFusion;user id=sa;password=123!@#qweasd;Connect Timeout=360; Asynchronous Processing=true";
            if (System.Web.HttpContext.Current.Cache["KOLtoTV"] == null)
            {
                DataSet uploadAccountTable1 = new DataSet();
                using (FileStream file = new FileStream(excelPath, FileMode.Open, FileAccess.Read))
                {
                    uploadAccountTable1 = NPOIHelper.ImportExceltoDs(file);
                }
                System.Web.HttpContext.Current.Cache.Insert("KOLtoTV", uploadAccountTable1, null, DateTime.MaxValue, TimeSpan.FromDays(1), CacheItemPriority.Normal, null);
            }
            if (System.Web.HttpContext.Current.Cache["TVtoKOL"] == null)
            {
                DataSet uploadAccountTable2 = new DataSet();
                using (FileStream file = new FileStream(excelPath2, FileMode.Open, FileAccess.Read))
                {
                    uploadAccountTable2 = NPOIHelper.ImportExceltoDs(file);
                }
                System.Web.HttpContext.Current.Cache.Insert("TVtoKOL", uploadAccountTable2, null, DateTime.MaxValue, TimeSpan.FromDays(1), CacheItemPriority.Normal, null);
            }

            return View();
        }
        public JsonResult GetKOL(
int page,
int limit)
        {
            ReturnResult result = new ReturnResult();
            DataSet d = System.Web.HttpContext.Current.Cache["TVtoKOL"] as DataSet;
            result.data = MVCUtility.ExtTable.GetPage(d.Tables[1], page, limit, out int allpage, out int count);
            result.count = count;

                return Json(result, JsonRequestBehavior.AllowGet);
        }
    
        public JsonResult GetTV(
int page,
int limit)
        {
            ReturnResult result = new ReturnResult();
            DataSet d = System.Web.HttpContext.Current.Cache["KOLtoTV"] as DataSet;

            result.data = MVCUtility.ExtTable.GetPage(d.Tables[2], page, limit, out int allpage, out int count);
            result.count = count;

                return Json(result, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetAccountName()
        {
            ReturnResult result = new ReturnResult();
            DataSet d = System.Web.HttpContext.Current.Cache["KOLtoTV"] as DataSet;
            result.data = d.Tables[3];
            result.count = d.Tables[0].Rows.Count;
            return Json(result, JsonRequestBehavior.AllowGet);

        }
        public JsonResult GetChannelName()
        {
            ReturnResult result = new ReturnResult();
            DataSet d = System.Web.HttpContext.Current.Cache["TVtoKOL"] as DataSet;
            result.data = d.Tables[3];
            result.count = d.Tables[0].Rows.Count;
            return Json(result, JsonRequestBehavior.AllowGet);

        }
        public JsonResult KOLtoTV(
int page,
int limit,
string keyword)
        {
            ReturnResult result = new ReturnResult();
            DataSet d = System.Web.HttpContext.Current.Cache["KOLtoTV"] as DataSet;
            int count;
            int allpage;
            if (!string.IsNullOrEmpty(keyword))
            {
                keyword = keyword?.Trim();
                var dt = ExtTable.GetDataFilter(d.Tables[0], $"AccountName = '{keyword}'");
                result.data = MVCUtility.ExtTable.GetPage(dt, page, limit, out allpage, out count);
                result.count = count;
            }
            else
            {
                result.data = MVCUtility.ExtTable.GetPage(d.Tables[0], page, limit, out allpage, out count);
                result.count = count;

            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        public JsonResult TVtoKOL(
int page,
int limit,
string keyword)
        {
            ReturnResult result = new ReturnResult();
            DataSet d = System.Web.HttpContext.Current.Cache["TVtoKOL"] as DataSet;
            int allpage;
            int count;
            if (!string.IsNullOrEmpty(keyword))
            {
                keyword = keyword?.Trim();
                var dt = ExtTable.GetDataFilter(d.Tables[0], $"ChannelName = '{keyword}'");
                result.data = ExtTable.GetPage(dt, page, limit, out allpage, out count);
                result.count = count;
            }
            else
            {
                result.data = ExtTable.GetPage(d.Tables[0], page, limit, out allpage, out count);
                result.count = count;

            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }



        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}